package oracle;

import java.util.StringTokenizer;
import java.io.*;
 

public class Oracle {
	
	public Oracle(String fichier){
		
		String ligne;
		
		try{
			InputStream ips = new FileInputStream(fichier); 
			InputStreamReader ipsr = new InputStreamReader(ips);
			LineNumberReader lnr = new LineNumberReader(ipsr);
			
			FileWriter ops = new FileWriter("z:\\marijo\\oracle.txt");

			while ((ligne = lnr.readLine()) != null) { 
				ops.write(revertChaine(ligne) + "\r\n");
			}
			
			ips.close();
			ops.close();
			
		}catch (Exception e){
			System.out.println(e.toString());
		}
		
	}

	private String revertChaine(String input){
		String valeurDec;
		StringTokenizer st = new StringTokenizer(input);
		String typeChaine = new String (st.nextToken());
		String result = new String();
		
		if(typeChaine.compareTo("F0") == 0){
			
			while(st.hasMoreTokens()){
				valeurDec = st.nextToken();
				result += valeurDec;
			}
			
			return result;
		}
		
		if(typeChaine.compareTo("F1") == 0){			
			String valeurHex;
			
			valeurDec = st.nextToken();
			valeurHex = String.format("%02X", Integer.parseInt(valeurDec));
			result += valeurHex;

			return result;
		}
		
		if(typeChaine.compareTo("F2") == 0){
			String valeurHex;
			
			for(int i = 0; i < 2; ++i) {
				valeurDec = st.nextToken();
				valeurHex = String.format("%02X", Integer.parseInt(valeurDec));
				result += valeurHex;
			}

			return result;
		}	
		
		if(typeChaine.compareTo("F3") == 0){
			String valeurHex;
			
			valeurDec = st.nextToken();
			valeurHex = String.format("%02X", Integer.parseInt(valeurDec));
			result = valeurHex + " ";
			
			valeurDec = st.nextToken();
			valeurHex = String.format("%02X", Integer.parseInt(valeurDec));
			result += valeurHex;

			return result;
		}
		
		return "Format non reconnu: <" + typeChaine + ">";
	}
}
